package org.redrock.test.interceptor.resolver;

import org.redrock.test.bean.JwtFormData;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import javax.servlet.http.HttpServletRequest;

public class JwtFormDataResolver implements HandlerMethodArgumentResolver {

    private String key = JwtFormData.class.toString();

    @Override
    public boolean supportsParameter(MethodParameter methodParameter) {
        return methodParameter.getParameterType().equals(JwtFormData.class);
    }

    @Override
    public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {
        HttpServletRequest request = nativeWebRequest.getNativeRequest(HttpServletRequest.class);
        JwtFormData jwtFormData = request.getSession().getAttribute(key) == null ? null : (JwtFormData) request.getSession().getAttribute(key);
        return jwtFormData;
    }
}
